package com.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;

import com.builder.ContactoBuilder;
import com.type.TipoTelefonoEnum;
import com.util.JSFUtil;
import com.util.ValidatorUtil;
import com.view.ContactoView;

@ManagedBean
@ViewScoped
public class ContactoController {

	private Logger log = Logger.getLogger(ContactoController.class);

	private List<ContactoView> listaContactos = null;
	private DataModel<ContactoView> contactoDM;
	private List<TipoTelefonoEnum> listaTipoTelefono;

	@ManagedProperty("#{contactoView}")
	private ContactoView view;

	@ManagedProperty("#{contactoBuilder}")
	private ContactoBuilder builder;

	public ContactoController() {
		listaContactos = new ArrayList<ContactoView>();
		contactoDM = new ListDataModel<ContactoView>();
		listaTipoTelefono = Arrays.asList(TipoTelefonoEnum.values());
	}

	public ContactoView getView() {
		return view;
	}

	public void setView(ContactoView view) {
		this.view = view;
	}

	public ContactoBuilder getBuilder() {
		return builder;
	}

	public void setBuilder(ContactoBuilder builder) {
		this.builder = builder;
	}

	public List<ContactoView> getListaContactos() {
		return listaContactos;
	}

	public void setListaContactos(List<ContactoView> listaContactos) {
		this.listaContactos = listaContactos;
	}

	public DataModel<ContactoView> getContactoDM() {
		return contactoDM;
	}

	public void setContactoDM(DataModel<ContactoView> contactoDM) {
		this.contactoDM = contactoDM;
	}

	public List<TipoTelefonoEnum> getListaTipoTelefono() {
		return listaTipoTelefono;
	}

	public void addContacto(ActionEvent event) {

		List<String> errores = null;

		try {
			errores = new ValidatorUtil().validate(view);
		} catch (Throwable t) {
			log.error("Error al validar", t);
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_ERROR,
							"Error al realizar la operacion", ""));

		}

		if (errores.size() == 0) {
			listaContactos.add(view);
			contactoDM = new ListDataModel<ContactoView>(listaContactos);
			view = new ContactoView();
		} else {
			JSFUtil.saveMessages(errores);
		}

	}

	public void deleteContacto(ActionEvent event) {
		ContactoView detalle = contactoDM.getRowData();
		listaContactos.remove(detalle);
		contactoDM = new ListDataModel<ContactoView>(listaContactos);
	}

	public void cerrar(ActionEvent event) {
		view = new ContactoView();
		listaContactos = new ArrayList<ContactoView>();
		contactoDM = new ListDataModel<ContactoView>();
	}

	public void clear() {
		view = new ContactoView();
		listaContactos = new ArrayList<ContactoView>();
		contactoDM = new ListDataModel<ContactoView>();
	}

}
